import Cocoa

//1.创建一个字典
var dict1:Dictionary<String,Double> = [:]
var dict2 = Dictionary<String,Double>()
var dict3:[String:Double] = [:]
var dict4 = [String:Double]()
var movieRatings = ["Donnie Darko":4,"ChungKing Express":5,"Dark City":4]; //key:value
let album = ["key1":"value1"]//不可变

print("I have reted \(movieRatings.count) movies")
let darkoRationg = movieRatings["Donnie Darko"]

movieRatings["Dark City"] = 5
movieRatings

let oldRating:Int? = movieRatings.updateValue(5, forKey: "Donnie Darko")    //返回该键之前的映射值，可空类型
if let lastRating = oldRating,let currentRating = movieRatings["Donnie Darko"]
{
    print("Old rating:\(lastRating);current rating:\(currentRating)")
}


//2.增加删除
movieRatings["The Cabinet of Dr.Caligari"] = 5
movieRatings

movieRatings.removeValue(forKey: "Dark City")
movieRatings

movieRatings["ChungKing Express"] = nil
movieRatings


//3.遍历
for (key,value) in movieRatings
{
    print("The movie \(key) was rated \(value).")
}

for movie in movieRatings.keys
{
    print("User has rated \(movie)")
}


//4.把字典转换为数组
let vatchedMovies = Array(movieRatings.keys)

